package sbmart.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.openymsg.network.FireEvent;
import org.openymsg.network.ServiceType;
import org.openymsg.network.Session;
import org.openymsg.network.event.SessionEvent;
import org.openymsg.network.event.SessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Albert
 */
public class LoggerYM implements SessionListener {

    public static final Logger LOG = LoggerFactory.getLogger("Yahoo");
    private Session session = new Session();
    private static List<String> users = new ArrayList();
    private static LoggerYM instance;

    private LoggerYM() {
        users.add("einstein.java");
        users.add("elva_ivana");
    }

    public static LoggerYM getLogger() {
        if (instance == null) {
            instance = new LoggerYM();
        }
        
        return instance;
    }
    
    public Session getSession(){
        return session;
    }
    

    public void doLogin() {
        try {
            session.login("root.ksusb", "1qaz2wsx", true, true);
            session.addSessionListener(this);
            session.sendMessage("einstein.java", "LOGIN");

        } catch (Exception e) {
            LOG.error("[ERORR]", e);
        }
    }

    public void send(String msg) {
        
        for (String user : users) {
            try {
                session.sendMessage(user, msg);
            } catch (IllegalStateException ex) {
                LOG.error("[ERORR]", ex);
            } catch (IOException ex) {
                LOG.error("[ERORR]", ex);
            }
        }
    }
    
    public void sendBuzz() {
         for (String user : users) {
            try {
                session.sendBuzz(user);
            } catch (IllegalStateException ex) {
                LOG.error("[ERORR]", ex);
            } catch (IOException ex) {
                LOG.error("[ERORR]", ex);
            }
        }
    }

    /*
     * this is my listener method it listen for YM message request
     */
    @Override
    public void dispatch(FireEvent fe) {
        ServiceType type = fe.getType();
        SessionEvent sessionEvent = fe.getEvent();

        if (type == ServiceType.MESSAGE) {
            try {
                // log request message
                String from = sessionEvent.getFrom();
                String msg = sessionEvent.getMessage();
                System.out.println("message from " + from + " : " + msg);

                if ("einstein.java".equals(from)) {

                    if ("logout".equals(msg)) {
                        session.logout();
                    }
                    
                    if("login".equals(msg)){
                        doLogin();
                    }
                } 
                
                session.sendMessage(sessionEvent.getFrom(), "echo: " + sessionEvent.getMessage());
                
            } catch (Exception e) {
                LOG.error("[ERORR]", e);
            }
        }
    }

    
}
